include("../../static/js/config.js");
include("../../static/js/common.js");


$(function () {
    var table, form, selectIds;
    var token = localStorage.getItem("token");

    var resourceId = getParam('resourceId');

    asyncRequest("/admin/authority/findAuthorityIdByResourceId", {resourceId:resourceId}, function (res) {
        if (res.code === 200){
            selectIds = res.data;
        }
    });

    var options = {
        url: config.server + '/admin/authority/list',
        method: 'POST',
        headers: {Authorization: 'Bearer ' + token},
        elem: '#table',
        page: true,
        curr: 1,
        count: 1,
        limit: 10,
        limits: [10, 20, 30, 40, 50, 60, 70, 80, 90],
        cols:[[
            {type:'checkbox'},
            {field:'name', title:'标识'},
            {field:'url', title:'地址'},
            {field:'createDate', title:'创建日期'},
        ]],
        request: {
            pageName: 'current',
            limitName: 'size'
        },
        parseData: function (res) {
            reqAfter(res, function (data) {});
            return {
                "code": res.code === 200 ? 0 : res.code,
                "msg": res.msg,
                "count": res.data.total,
                "data": res.data.records
            };
        },
        done: function (res) {

            // 禁止全选
            var all = $('th[data-field="0"] input[type="checkbox"]');
            all.attr('disabled', 'disabled');
            all.next().addClass('layui-btn-disabled');

            var list = res.data;
            if (list && selectIds){
                for (var i = 0; i < list.length; i++){
                    if (selectIds.includes(list[i].id)) {
                        list[i]["LAY_CHECKED"] = 'true';
                        //找到对应数据改变勾选样式
                        var index= list[i]['LAY_TABLE_INDEX'];
                        $('tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                        $('tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                    }
                }
            }
        }
    };

    layui.use(['table', 'form'], function () {
        form = layui.form;
        table = layui.table;
        table.render(options);

        table.on('checkbox(optEvent)',function(obj){
            if (obj.type === 'all'){
                return;
            }

            if (obj.checked){
               request("/admin/resource/addAuthority", {id: resourceId, authorityId: obj.data.id}, function (res) {
                   if (res.code === 200){
                       layer.msg(res.msg, {icon: 1});
                   } else {
                       layer.msg(res.msg, {icon: 5});
                   }
               })
            } else {
                request("/admin/resource/delAuthority", {id: resourceId, authorityId: obj.data.id}, function (res) {
                    if (res.code === 200){
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                })
            }
        });
    });
});